package cn.edu.zut.score.controller.admin;

import cn.edu.zut.score.core.constant.GroupInfoEnum;
import cn.edu.zut.score.core.domain.PageResponse;
import cn.edu.zut.score.core.domain.RestResponse;
import cn.edu.zut.score.core.dto.request.GroupAddRequest;
import cn.edu.zut.score.core.dto.request.GroupPageRequest;
import cn.edu.zut.score.core.dto.response.GroupResponse;
import cn.edu.zut.score.core.entity.Info;
import cn.edu.zut.score.service.IGroupInfoService;
import cn.edu.zut.score.service.IGroupService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zby
 * @date 2021/06/28
 */
@RestController
@RequestMapping("/admin/group")
@ApiModel("小组相关api")
@CrossOrigin
public class AdminGroupController {

    @Autowired
    private IGroupService iGroupService;

    @Autowired
    private IGroupInfoService iGroupInfoService;

    @ApiOperation("新建小组")
    @PostMapping("")
    public RestResponse<GroupResponse> addGroup(@RequestBody GroupAddRequest groupAddRequest) {
        return iGroupService.saveGroup(groupAddRequest);
    }

    @ApiOperation("增加小组成员")
    @PutMapping("")
    public RestResponse<GroupResponse> addGroupNumber(@RequestBody GroupAddRequest groupAddRequest) {
        return iGroupService.saveGroup(groupAddRequest);
    }

    @ApiOperation("删除小组成员")
    @DeleteMapping("")
    public RestResponse<GroupResponse> removeGroupNumber(@RequestBody GroupAddRequest groupAddRequest) {
        return iGroupService.removeGroupNumber(groupAddRequest);
    }

    @ApiOperation("获取小组信息")
    @GetMapping("/{groupid}")
    public RestResponse<GroupResponse> getGroupById(@PathVariable("groupid") Long groupid) {
        return iGroupService.getGroupById(groupid);
    }

    @ApiOperation("分页获取小组信息（不包括个人小组）")
    @PostMapping("/list")
    public RestResponse<PageResponse<GroupResponse>> getGroups(@RequestBody GroupPageRequest groupPageRequest) {
        return iGroupService.getGroups(groupPageRequest);
    }

    @ApiOperation("查看所有小组（不包括个人小组）")
    @GetMapping("/all")
    public RestResponse<List<Info>> queryAllGroups() {
        return iGroupService.queryAllGroups();
    }

    @ApiOperation("解散小组")
    @DeleteMapping("/{groupid}")
    public RestResponse<Long> removeGroup(@PathVariable("groupid") Long groupid) {
        return iGroupService.removeGroup(groupid);
    }

    @ApiOperation("根据Ids解散小组")
    @DeleteMapping("/list")
    public RestResponse<Integer> removeGroups(@RequestBody List<Long> allGroupIds) {
        return iGroupService.removeGroupsById(allGroupIds);
    }

    @ApiOperation("查看所有小组组长")
    @GetMapping("/leader/all")
    public RestResponse<List<Info>> queryAllLeaders() {
        return iGroupInfoService.queryAllLeadersByRole(GroupInfoEnum.leader);
    }

    @ApiOperation("查看除小组成员外教师")
    @GetMapping("/remain/{groupid}")
    public RestResponse<List<Info>> queryAllRemainTeachers(@PathVariable("groupid") Long groupid) {
        return iGroupInfoService.queryAllRemainTeachers(groupid);
    }

}
